﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace STR
{
    public partial class Form1 : Form
    {

        Screen Rift;
        //Process GameProcess;

        [DllImport("user32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        public Form1()
        {
            InitializeComponent();
            PopulateList();

            RiftID.Text = GetRift();


          //   GetRift()
        }

        public string GetRift()
        {
            Screen[] screens = Screen.AllScreens;

            foreach (Screen thescreen in screens)
            {
                if ((thescreen.Bounds.Width == 1280) && (thescreen.Bounds.Height == 800))
                {
                    Rift = thescreen;
                    Console.WriteLine("Found Rift!  Device:{0}", thescreen.DeviceName);
                    return "Rift Found!";
                }
            }
            return "RIFT NOT FOUND!";
        }

        public void PopulateList()
        {
            Process[] AllProcess = Process.GetProcesses();
            IEnumerable<Process> filteredProcess =
                from p in AllProcess
                    where p.MainWindowTitle != ""
                    select p;

            processBox.DataSource = filteredProcess.ToArray();
            
            
        }

        public void DoIt()
        {
            const short SWP_NOMOVE = 0X2;
            const short SWP_NOSIZE = 1;
            const short SWP_NOZORDER = 0X4;
            const int SWP_SHOWWINDOW = 0x0040;
            const int SW_SHOWNORMAL = 1;
            const int SW_SHOWMINIMIZED = 2;
            const int SW_SHOWMAXIMIZED = 3;

            Process p = (Process)processBox.SelectedItem;
            IntPtr h = p.MainWindowHandle;


            SetWindowPos(h, 0, Rift.Bounds.X, Rift.Bounds.Y, 0, 0, SWP_NOZORDER | SWP_SHOWWINDOW);
            ShowWindowAsync(h, SW_SHOWMAXIMIZED);
            SwitchToThisWindow(h, true);
            
        }
        

        private void refresh_Click(object sender, EventArgs e)
        {
            PopulateList();
        }

        private void SendToRift_Click(object sender, EventArgs e)
        {
            DoIt();
        }

    }
}
